package com.guoguo.sincemusic.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.NestedScrollView
import com.guoguo.sincemusic.ui.MainActivity
import com.guoguo.sincemusic.databinding.FragmentFavoritesBinding
import com.guoguo.sincemusic.ui.common.BaseFragment

/**
 * @author GuoGuo
 * @date 2022/2/17
 */
class FavoritesFragment : BaseFragment() {

    private lateinit var binding: FragmentFavoritesBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentFavoritesBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun initListener() {
        super.initListener()
        with(binding){
            //   滑动事件
            nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
                if (scrollY < oldScrollY) { // up
                    MainActivity.mainActivity?.toNavigation(false)
                    animateSearchBar(false)
                }
                if (scrollY > oldScrollY) { // down
                    MainActivity.mainActivity?.toNavigation(true)
                    animateSearchBar(true)
                }
            })
            // 打开抽屉栏
            searchBarView.btMenu.setOnClickListener {
                MainActivity.mainActivity?.openDrawer()
            }
        }
    }
    
    // 搜索栏动画
    var isSearchBarHide = false
    private fun animateSearchBar(hide: Boolean) {
        val search_bar = binding.searchBarView.searchBar
        if (isSearchBarHide && hide || !isSearchBarHide && !hide) return
        isSearchBarHide = hide
        val moveY = if (hide) -(2 * search_bar.height) else 0
        search_bar.animate().translationY(moveY.toFloat()).setStartDelay(100).setDuration(300)
            .start()
    }
}